package com.hmdp.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hmdp.dto.Result;
import com.hmdp.entity.ShopType;
import com.hmdp.mapper.ShopTypeMapper;
import com.hmdp.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.utils.SystemConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.el.stream.Stream;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
@Slf4j
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public Result getShopTypeList() {
        String key= SystemConstants.SHOP_TYPE_LIST;
        String shoptype = stringRedisTemplate.opsForValue().get(key);
        if (StrUtil.isNotBlank(shoptype)){
            List<ShopType> shopTypes= JSONUtil.toList(shoptype, ShopType.class);
            System.out.println(shopTypes.toString());
            return Result.ok(shopTypes);
        }
        List<ShopType> list = query().orderByAsc("sort").list();
        if (list == null || list.size() == 0){
            return Result.fail("未查询到数据");
        }
        stringRedisTemplate.opsForValue().set(key,JSONUtil.toJsonStr(list));
        return  Result.ok(list);
    }
}
